#include"List.h"
LTNode* LTBuyNode(LTDataType x)
{
	LTNode* node = (LTNode*)malloc(sizeof(LTNode));
	if (node == NULL)
	{
		perror("malloc");
		exit(1);
	}
	node->data = x;
	node->next = node->prev = node;
	return node;

}
void LTInit(LTNode** hhead)
{
	*hhead = LTBuyNode(-1);
}
void LTPushBack(LTNode* head, LTDataType x)
{
	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));

	newnode->prev = head->prev;
	newnode->next = head;

	head->prev->next = newnode;
	head->prev = newnode;
}

void LTPushFront(LTNode* head, LTDataType x)
{
	assert(head);
	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));

	newnode->next = head->next;
	newnode->prev = head;

	head->next->prev = newnode;
	head->next = newnode;
}

void LTPrint(LTNode* head)
{
	LTNode* pcur = head->next;
	while (pcur != head)
	{
		printf("%d->", pcur->data);
		pcur = pcur->next;
	}
	printf("\n");
}

void LTPopBack(LTNode* head)
{
	assert(head && head->next != head);
	LTNode* tail = head->prev;

	tail->prev->next = head;
	head->prev = tail->prev;

	free(tail);
	tail = NULL;
}
void LTPopFront(LTNode* head)
{
	assert(head && head->next != head);

	head->next->next->prev = head;
	head->next = head->next->next;
	
	free(head->next);
}
void LTInset(LTNode* pos, LTDataType x)
{
	assert(pos);
	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
	newnode->next = pos->next;
	newnode->prev = pos;

	pos->next->prev = newnode;
	pos->next = newnode;
}
void LTEtase(LTNode* pos)
{
	assert(pos);
	pos->next->prev = pos->prev;
	pos->prev->next = pos->next;

	free(pos);
	pos = NULL;
}
LTNode* LTFind(LTNode* head, LTDataType x)
{
	LTNode* pcur = head->next;
	while (pcur->next != head)
	{
		if (pcur->data == x)
			return pcur;
		pcur = pcur->next;
	}
	return NULL;
}